package vn.sunnet.backviet01.game.coloa.human.enemy;

import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

import android.util.Log;

import vn.sunnet.backviet01.game.coloa.constant.Constants;

public class Range extends Enemy {
	private static long durationMove = 100;
	private static long durationAttack = 120;
	private static long durationDie = 150;
	
	private long countDelay = 0;

	public Range(int tiledAttack, float pX, float pY,
			TiledTextureRegion pTiledTextureRegion) {
		super(tiledAttack, pX, pY, pTiledTextureRegion);
		this.name = Constants.RANGE;
	}

	public void onManagedUpdate(float pSecondsElapsed) {
		if (isAllowChangeStatus) {
			stopAnimation();
			if (this.status == Constants.DIE) {
				updateTextureRegion(mTextureRegion_next, true);
				animate(durationDie);
			} else if (isFreezed && countDelay == 0) {
				updateTextureRegion(mTextureRegion_next, true);
				countDelay = System.currentTimeMillis();
				this.handler.setEnabled(false);
				if (status == Constants.MOVE)
					this.setPosition(this.getX() + 20, this.getY());
			} else if (isStun && startStun == 0) {
				startStun = System.currentTimeMillis();
				setStunAction();
			}  else if (this.status == Constants.ATTACK) {
				updateTextureRegion(mTextureRegion_next, true);
				animate(durationAttack);
				this.setPosition(this.getX() - 35, this.getY() + 13);
			}
			isAllowChangeStatus = false;
		} else if (isFreezed && this.hitPoints > 0) {
			if (countDelay + timeFreezed < System.currentTimeMillis()) {
				resetMove();
				isFreezed = false;
				countDelay = 0;
			}
		} else if (isStun && this.hitPoints > 0) {
			if (timeStun + startStun < System.currentTimeMillis()) {
				resetStunAction();
				isStun = false;
				startStun = 0;
			}
		}
		
		if (status == Constants.ATTACK
				&& this.getCurrentTileIndex() != tiledAttack)
			isAttack = false;

		else if (status == Constants.DIE && this.life) {
			setAlpha(getAlpha() - 0.02f);
		}
		xContain = this.getX() + 5;
		yContain = this.getY() + 10;
		
		super.onManagedUpdate(pSecondsElapsed);
	}
	
	protected void createContain() {
		xContain = this.getX() + 5;
		yContain = this.getY() + 10;

		widthContain = this.getWidth() - 10;
		heightContain = this.getHeight() - 15;
	}

	
	public float getxContain() {
		return xContain + getX();
	}

	public float getyContain() {
		return yContain + this.getY();
	}

	public void resetMove() {
		updateTextureRegion(mTextureRegionMove, true);
		if (status == Constants.MOVE)
			this.setPosition(this.getX() - 20, this.getY());
		else if (status == Constants.ATTACK) 
			this.setPosition(getX() + 20, getY());
		this.animate(durationMove);
		handler.setEnabled(true);
		status = Constants.MOVE;
	}
	

	public void setStunAction() {
		if (isFreezed) {
			isStun = false;
			return;
		}
		int index = this.getCurrentTileIndex();
		Log.e("index 105", ""+index);
		stopAnimation(index);
		switch (status) {
		case Constants.MOVE:
			animate((long)(durationMove * 2));
			handler.setVelocityX(handler.getVelocityX() / 2);
			setCurrentTileIndex(index, true);
			
			break;
		case Constants.ATTACK:
			animate((long)(durationAttack * 2));
			setCurrentTileIndex(index, true);
			break;
		default:
			break;
		}
	}
	
	public void resetStunAction() {
		if (isFreezed) {
			isStun = false;
			return;
		}
		int index = this.getCurrentTileIndex();
		Log.e("index 129", ""+index);
		stopAnimation(index);
		switch (status) {
		case Constants.MOVE:
			animate(durationMove);
			handler.setVelocityX(handler.getVelocityX() * 2);
			setCurrentTileIndex(index, true);
			Log.e("index 136", ""+getCurrentTileIndex());
			break;
		case Constants.ATTACK:
			animate(durationAttack);
			setCurrentTileIndex(index, true);
			Log.e("index 140", ""+getCurrentTileIndex());
			break;
		default:
			break;
		}
	}
}
